
/*
 * @(#)WorkflowStatusFlagTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.taglib;

import java.sql.Connection;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.WebUtil;
import com.dcivision.workflow.bean.MtmWorkflowProgressUserRecord;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.core.WorkflowRetrievalManager;

/**
 * @author           Tony Chen
 * @company          DCIVision Limited
 * @creation date    13/10/2005
 * @version          $Revision: 1.5.2.3 $
 */
public class WorkflowStatusFlagTag extends BodyTagSupport {
  public static final String REVISION = "$Revision: 1.5.2.3 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());

  private WorkflowProgress workflowProgress = null;
  private String inProgressIcon = null;
  private String completedIcon = null;
  private String showActor = null;
  
  
	public String getShowActor() {
		return showActor;
	}
  
	public void setShowActor(String showActor) {
		this.showActor = showActor;
	}
  
	public WorkflowProgress getWorkflowProgress() {
		return workflowProgress;
	}
  
	public void setWorkflowProgress(WorkflowProgress workflowProgress) {
		this.workflowProgress = workflowProgress;
	}
  
	public String getCompletedIcon() {
		return completedIcon;
	}
  
	public void setCompletedIcon(String completedIcon) {
		this.completedIcon = completedIcon;
	}
  
	public String getInProgressIcon() {
		return inProgressIcon;
	}
  
	public void setInProgressIcon(String inProgressIcon) {
		this.inProgressIcon = inProgressIcon;
	}
  
  public WorkflowStatusFlagTag(){
    super();
  }
    
  public int doStartTag() throws JspException {
    try {
      init();
      pageContext.getOut().print(this.getContent());//Edit by TC for Ajax-Used
    } catch (ApplicationException ae){
      log.error(ae.getMsgCode(), ae);
    } catch (Exception e) {
      log.fatal(e.getMessage(), e);
    } finally {
      this.release();
    }
    return(SKIP_BODY);
  }

  public int doEndTag() throws JspException {
    return EVAL_PAGE;
 }

  public void release() {
    super.release();
    this.setWorkflowProgress(null);
    this.setInProgressIcon(null);
    this.setCompletedIcon(null);
    this.setShowActor(null);
  }

  protected boolean validateTag(){
    return ( workflowProgress != null);
  }

  protected String getContent() throws Exception {
    //JspWriter out = pageContext.getOut();    
    StringBuffer content = new StringBuffer();
      
    //Pending
    if (WorkflowProgress.STATUS_PENDING.equals(this.getWorkflowProgress().getStatus()) ) {
      content.append("&nbsp;");
      
    //In Progress  
    } else if (WorkflowProgress.STATUS_IN_PROGRESS.equals(this.getWorkflowProgress().getStatus())) {
      
      String userName = "<b style=\\'font-size:11px\\'>";
      List userNameList = (workflowProgress).getPickUpUserName();
      for (int n = 0; n < userNameList.size(); n++ ) {
        userName += "<img src=\\'" + WebUtil.getImage(pageContext.getRequest(), "IconUser.gif")+"\\' align=\\'absmiddle\\' border=\\'0\\'>" + userNameList.get(n) + "<br>";
      }
      userName += "</b>";
    	
      if (!Utility.isEmpty(userNameList)) {
        content.append (" <a id=\"task\" href=\"javascript://\"   style=\"cursor:text \"  ");
        content.append (" onmouseover=\"return getPopupInfo(\' " + userName + "\');\" onmouseout=\"return nd();\" >" );
      }       
      
      content.append ( "<img src=\"" + WebUtil.getImage(pageContext.getRequest(), this.inProgressIcon) + "\" align=\"absmiddle\" border=\"0\">" );
      
      if (!Utility.isEmpty(userNameList)) {
        content.append ( "</a>");
      }
      
    //Completed
    } else {
      if (GlobalConstant.TRUE.equals(this.showActor)) {
        content.append ("<a href=\'#\' ");
        content.append ("onmouseover=\"return getPopupInfo('");
        content.append (TextUtility.escapeJSString(getActorNameDescription()));
        
        content.append ("');\" ");
        content.append ("onmouseout=\"return nd();\"> ");
        
        content.append ( "<img src=\"" + WebUtil.getImage(pageContext.getRequest(), this.completedIcon) + "\" align=\"absmiddle\" border=\"0\">" );
        content.append("</a>");
      } else {
        content.append ( "<img src=\"" + WebUtil.getImage(pageContext.getRequest(), this.completedIcon) + "\" align=\"absmiddle\" border=\"0\">" );
      }
    }
    return content.toString();
    //out.println(content.toString());
  }

  protected void init() throws ApplicationException {    
    if ( !validateTag() ){
      throw new IllegalArgumentException();
    }
  }
 
  private String getActorNameDescription() throws ApplicationException {
    StringBuffer sb = new StringBuffer();
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection) pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    
    WorkflowRetrievalManager retrievalManager = new WorkflowRetrievalManager(sessionContainer, conn);
    
    List userActorList = retrievalManager.getWorkflowProgressUserRecordListByProgressID( this.workflowProgress.getID());
         
    String iconName = null;
    
    sb.append("<b><u style=\'font-size:11px\'>");
    sb.append(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "alert.message.actor_name"));
    sb.append(":</u></b><br>");
    
    for (int j=0; j<userActorList.size(); j++) {
      MtmWorkflowProgressUserRecord wfProgressUserRecord = (MtmWorkflowProgressUserRecord)userActorList.get(j);
      iconName = "IconUser.gif";  
      if (j > 0) {
        sb.append("<br>");
      }
      sb.append("<img src=\'" + WebUtil.getImage(pageContext.getRequest(), iconName) + "\' align=\'absmiddle\' border=\'0\'>" + UserInfoFactory.getUserFullName(wfProgressUserRecord.getUserRecordID()) );
    }
    
    return sb.toString(); 
  }
  
}


